package com.eastfair.imaster.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.echo.core.EchoService;
import com.eastfair.imaster.dto.SuggestDTO;
import com.eastfair.imaster.dto.SuggestPageQuery;
import com.eastfair.imaster.dto.SuggestSaveDTO;
import com.eastfair.imaster.dto.SuggestUpdateDTO;
import com.eastfair.imaster.entity.Suggest;
import com.eastfair.imaster.service.SuggestService;
import com.eastfair.imaster.vo.SuggestVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 *
 * </p>
 *
 * @author zjn
 * @date 2021-08-31
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/suggest")
@Api(value = "Suggest", tags = "")
public class SuggestController extends SuperController<SuggestService, Long, Suggest, SuggestPageQuery, SuggestSaveDTO, SuggestUpdateDTO> {

    @Autowired
    private EchoService echoService;
    @Autowired
    private SuggestService suggestService;

    @Override
    public void handlerResult(IPage<Suggest> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<Suggest> suggestList = list.stream().map((map) -> {
            Suggest suggest = Suggest.builder().build();
            //TODO 请在这里完成转换
            return suggest;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(suggestList));
    }

    /**
     * 新增意见反馈
     *
     * @param suggestDTO 意见反馈实体
     * @return 是否插入成功
     */
    @ApiOperation(value = "新增意见反馈", notes = "新增意见反馈")
    @PostMapping("/save")
    public R<Boolean> save(@RequestBody @Validated({SuggestDTO.VerifyInsert.class}) SuggestDTO suggestDTO) {
        return R.success(suggestService.save(suggestDTO));
    }

    /**
     * 回复
     *
     * @param suggestDTO 意见反馈实体
     * @return 是否修改成功
     */
    @ApiOperation(value = "回复", notes = "回复")
    @PostMapping("/replyById")
    public R<Boolean> replyById(@RequestBody @Validated({SuggestDTO.VerifyUpdate.class}) SuggestDTO suggestDTO) {
        return R.success(suggestService.replyById(suggestDTO));
    }

    /**
     * 通过id获取意见反馈实体
     *
     * @param id 意见反馈主键
     * @return 意见反馈实体
     */
    @ApiOperation(value = "通过id获取意见反馈实体", notes = "通过id获取意见反馈实体")
    @GetMapping("/{id}")
    public R<SuggestVO> queryById(@PathVariable("id") Long id) {
        return R.success(suggestService.queryById(id));
    }

    /**
     * 获取分页意见反馈列表
     *
     * @param params 分页条件
     * @return 分页意见反馈列表
     */
    @ApiOperation(value = "获取分页意见反馈列表", notes = "获取分页意见反馈列表")
    @PostMapping("/queryPageList")
    public R<Page<SuggestVO>> queryPageList(@RequestBody PageParams<SuggestPageQuery> params) {
        return R.success(suggestService.queryPageList(params));
    }

    /**
     * 根据用户获取分页意见反馈列表
     *
     * @param params 分页条件
     * @return 分页意见反馈列表
     */
    @ApiOperation(value = "根据用户获取分页意见反馈列表", notes = "根据用户获取分页意见反馈列表")
    @PostMapping("/queryPageListBySubjectType")
    public R<Page<SuggestVO>> queryPageListBySubjectType(@RequestBody PageParams<SuggestPageQuery> params) {
        return R.success(suggestService.queryPageListBySubjectType(params));
    }

}
